package com.xk857.simpleeffect.dialog;

import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;

import androidx.annotation.NonNull;

import java.util.Calendar;

public class CustomDatePickerDialog extends DatePickerDialog {
    private final Calendar mMinDate;

    public CustomDatePickerDialog(Context context, int themeResId, OnDateSetListener listener, int year, int month, int dayOfMonth,
                                  Calendar minDate) {
        super(context, themeResId, listener, year, month, dayOfMonth);
        mMinDate = minDate;
    }

    @Override
    public void onDateChanged(@NonNull DatePicker view, int year, int month, int dayOfMonth) {
        Calendar selected = Calendar.getInstance();
        selected.set(year, month, dayOfMonth);

        if (mMinDate != null && selected.before(mMinDate)) {
            view.init(mMinDate.get(Calendar.YEAR), mMinDate.get(Calendar.MONTH), mMinDate.get(Calendar.DAY_OF_MONTH), this);
        } else {
            view.init(year, month, dayOfMonth, this);
        }
    }
}